package com.zzyl.nursing.service.impl;

import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.zzyl.common.exception.base.BaseException;
import com.zzyl.common.utils.StringUtils;
import com.zzyl.nursing.service.WechatService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * 微信小程序工具组件实现类
 *
 * @Author mr.zhao
 * @Date 2025/5/23 16:38
 */
@Service
public class WechatServiceImpl implements WechatService {

	@Value("${wechat.appId}")
	private String appId; //小程序id

	@Value("${wechat.appSecret}")
	private String appSecret; //小程序秘钥

	/**
	 * 获取openId的请求地址
	 */
	private static final String GET_OPEN_ID_URL = "https://api.weixin.qq.com/sns/jscode2session";

	/**
	 * 获取接口调用凭证的请求地址
	 */
	private static final String GET_ACCESS_TOKEN_URL = "https://api.weixin.qq.com/cgi-bin/token";

	/**
	 * 获取手机号的请求地址
	 */
	private static final String POST_GET_PHONE_NUM_URL = "https://api.weixin.qq.com/wxa/business/getuserphonenumber?access_token=";


	/**
	 * 封装公共请求参数
	 * @return
	 */
	private Map<String,Object> getBaseParam(){
		Map<String,Object> paramMap = new HashMap<>();
		paramMap.put("appid",appId);
		paramMap.put("secret",appSecret);
		return paramMap;
	}

	/**
	 * 根据临时凭证码(openId获取专用凭证码)获取openId
	 *
	 * @param code
	 * @return
	 */
	@Override
	public String getOpenId(String code) {
		//1. 准备请求参数
		Map<String,Object> paramMap = getBaseParam();
		paramMap.put("js_code",code);
		paramMap.put("grant_type","authorization_code");

		//2. 使用hutool工具发起get请求调用小程序登录接口
		String resultJSON = HttpUtil.get(GET_OPEN_ID_URL, paramMap);
		if (StringUtils.isBlank(resultJSON)){
			throw new BaseException("调用微信小程序登录接口失败");
		}

		//3. 解析获取响应结果中的openId
		JSONObject jsonObject = JSON.parseObject(resultJSON);

		Integer errcode = jsonObject.getInteger("errcode");
		if (errcode != null && errcode != 0){
			throw new BaseException("调用微信小程序登录接口失败");
		}

		return jsonObject.getString("openid");
	}

	/**
	 * 根据临时凭证码(手机获取专用凭证码,非openId凭证码)获取手机号
	 *
	 * @param code
	 * @return
	 */
	@Override
	public String getPhone(String code) {
		//1. 准备请求参数
		Map<String,Object> paramMap = new HashMap<>();
		paramMap.put("code",code);

		//2. 获取接口调用凭证
		String accessToken = this.getAccessToken();

		//3. 使用hutool工具发起post请求微信获取手机号接口
		String resultJSON = HttpUtil.post(POST_GET_PHONE_NUM_URL + accessToken, JSON.toJSONString(paramMap));
		if (StringUtils.isBlank(resultJSON)){
			throw new BaseException("调用微信获取手机号接口失败");
		}

		//4. 解析响应结果,得到phoneNumber手机号
		JSONObject jsonObject = JSON.parseObject(resultJSON);
		Integer errcode = jsonObject.getInteger("errcode");
		if (errcode != null && errcode != 0){
			throw new BaseException("调用微信获取手机号接口失败");
		}

		String phoneInfo = jsonObject.getString("phone_info");
		JSONObject phoneInfoObject = JSON.parseObject(phoneInfo);

		return phoneInfoObject.getString("phoneNumber");
	}

	/**
	 * 获取接口凭证
	 * @return
	 */
	private String getAccessToken(){
		//1. 准备请求参数
		Map<String,Object> paramMap = getBaseParam();
		paramMap.put("grant_type","client_credential");

		//2. 使用hutool工具发起get请求微信获取接口调用凭证接口
		String resultJSON = HttpUtil.get(GET_ACCESS_TOKEN_URL, paramMap);

		//3. 解析响应结果,得到access_token接口访问凭证
		if (StringUtils.isBlank(resultJSON)){
			throw new BaseException("调用获取接口凭证的接口失败");
		}
		JSONObject jsonObject = JSON.parseObject(resultJSON);
		Integer errcode = jsonObject.getInteger("errcode");
		if (errcode != null && errcode != 0){
			throw new BaseException("调用获取接口凭证的接口失败");
		}

		return jsonObject.getString("access_token");
	}
}
